#ifndef UIMANAGER_H_
#define UIMANAGER_H_

#include "Point2d.h"
#include "MouseEvent.h"
#include "Window.h"
#include "Timer.h"
#include "UICallback.h"
#include "UITextEntry.h"
#include "KeyboardEvent.h"

#include <list>

namespace Lame3d
{	
	class UIManager
	{
		public:
			UIManager();
			~UIManager();
			
			void AddWindow(Window *w);
			void RemoveWindow(Window *w);
			
			void RegisterCallback(UICallback *c);
			void UnregisterCallback(UICallback *c);
			void UnregisterCallback(const std::string &s);
			
			void Handle(const MouseMotionEvent &evt);
			/* Return true if the UIManager consumed the event. */
			bool Handle(const MouseClickEvent &evt);
			bool Handle(const KeyEvent &evt);
			
			std::list<Window*>::const_iterator WindowBegin() const;
			std::list<Window*>::const_iterator WindowEnd() const;
			
			float KeyRepeatSeconds() const;
			void KeyRepeatSeconds(float f);
			
			float KeyRepeatDelay() const;
			void KeyRepeatDelay(float f);
			
		protected:
			std::list<Window*> windowList;
			
			Window* draggedWindow;
			UIElement* pressedElement;
			UITextEntry* activeTextEntry;
			bool dragDown;
			bool resizeXPositive;
			bool resizeYPositive;
			bool resizeDown;
			
			Timer fadeTimer;
			std::list<Window*> newWindows;
			std::list<Window*> fadingWindows;
			
			std::list<UICallback*> callbacks;
			
			Timer keyRepeatTimer;
			KeyEvent *repeatEvent;
			float keyRepeatDelay;
			float delayCounter;
			float keyRepeatSeconds;
			float repeatCounter;
			
			Window* selectWindow(const Point2d &pt) const;
			UIElement* selectElement(const Point2d &pt) const;
			void raiseWindow(Window *w);
			
			void fadeWindows();
			
			void emit(Window &w, UIElement &e, const std::string &message);
	};
}

#endif /*UIMANAGER_H_*/
